package mop.controller;

import java.util.List;

import mop.exception.BusinessRule2Exception;
import mop.exception.BusinessRule3Exception;
import mop.exception.IllegalIdException;
import mop.exception.RecursiveBusinessRule2Exception;
import mop.model.World;
import mop.model.task.Task;
import mop.model.task.TaskInterface;

/**
 * UpdateTaskStatusController
 * 
 * @author MOP Team 7
 */
public class UpdateTaskStatusController extends UseCaseController
{
	/**
	 * Creates a UpdateTaskStatusController.
	 * @see UseCaseController#UseCaseController(World)
	 */
	public UpdateTaskStatusController(World world)
	{
		super(world);
	}
	
	/**
	 * Return a list with all existing tasks
	 * @return a list of interfaces to all tasks in system
	 */
	public List<TaskInterface> getAllTasks()
	{
		return super.getAllTasks();
	}
	
	
	/**
	 * Update the status of a task
	 * @param task		The id of the task
	 * @param newstatus	The new status for the task
	 * @throws IllegalIdException				If there is no task with given id
	 * @throws BusinessRule2Exception			If business rule 2 is violated		
	 * @throws BusinessRule3Exception			If business rule 3 is violated
	 * @throws RecursiveBusinessRule2Exception	If business rule 2 is violated, with expanded error message
	 */
	public void updateTaskStatus(String task,String newstatus) 
		throws IllegalIdException, BusinessRule2Exception, BusinessRule3Exception, 
				RecursiveBusinessRule2Exception
	{
		Task t = getTask(task);
		t.updateStatus(newstatus);
	}
	
	/**
	 * After a business rule 2 violation, update status for all inconsistent tasks
	 * @pre UpdateTaskStatus has been called
	 * 
	 * @param task		
	 * 		  The id of the task
	 * @param newstatus	
	 * 		  The new status for the task
	 * 
	 * @throws IllegalIdException 	
	 * 		   If there is no task with given id
	 * 			|super#getTask(String)
	 */
	public void alternateFlowThreeYes(String task,String status) 
		throws IllegalIdException
	{
		 Task t = getTask(task);
		 t.forceStatus(status);
		 for(TaskInterface ti :  t.getAllParentList())
		 {
			 /** sets it to the correct state **/
			ti.getStatus(); 
		 }
	}

	
	/***************************************************************************
	 * 								Visit
	 ***************************************************************************/
	
	/**
	 * Make a CallBack to the visitor
	 * @param v the visitor
	 */
	public void visit(ControllerVisitor v)
	{
		v.action(this);
	}
	
}
